import { onUnmounted, ref } from 'vue'
import dayjs from 'dayjs'
// 倒计时格式化函数
export const useCountDown = () => {
  const formatTime = ref(0)
  let timer = null
  const start = (num) => {
    timer = setInterval(()=>{
       num--
       formatTime.value = dayjs.unix(num).format('mm分ss秒')
       // 倒计时结束清空计时器
       if(num <= 0){
        clearInterval(timer)
       }
    },1000)
  }
  onUnmounted(()=>{
    timer && clearInterval(timer)
  })
  return {formatTime, start}
}
